/**
 * 
 */
package com.zc.springproject.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.springframework.stereotype.Repository;

import com.zc.springproject.javabean.User;

/**
 * @author ZhangCheng
 * @time 2019年10月31日
 */
@Repository
public class UserDAO extends BasicDAO<User> {
	/**
	 * 新增新用户，并插入到数据库中，参数是User对象
	 * 
	 * @throws SQLException
	 */
	public int insertNewUser(User u) throws SQLException {
		int count = 0;
		String sql = "Insert into user values(?,?,?,?)";

		count = super.updateDML(sql, u.getName(), u.getAge(),u.getPwd(), u.getPhoneNum(), u.getEmail());

		return count;
	}

	/**
	 * getUserInfoById(String name,boolean excludeDelete) 通过用户Id获取该用户所有信息
	 */
	public User getUserInfoById(String name) {
		StringBuffer sql = new StringBuffer("select * from user where name = ? ");

		ArrayList<User> u = getInfoFromDB(sql.toString(), name);
		if (u.size() == 0) {
			return null;
		} else {
			return u.get(0);
		}

	}

	@Override
	public ArrayList<User> resultSetToArrayList(ResultSet re) {
		ArrayList<User> user = new ArrayList<User>();

		try {
			while (re.next()) {
				user.add(new User(re.getString("name"), re.getInt("age"), re.getString("pwd"), re.getString("phone"),
						re.getString("email")));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return user;
	}

}
